package com.ax.api.route.base.common;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.ax.api.route.util.RouteConfig;

@RestController
@RequestMapping("/basePharmacist")
public class BasePharmacistRoute {

	 @Autowired
	 LoadBalancerClient loadBalancerClient;
	 @Autowired
	 RestTemplate restTemplate;
	
	 
	 /**
	 * 菜单页面的跳转
	 */
	@GetMapping("/sys/doctor")
	String doctor(Model model) {
		String url = geturl("/sys/doctorPharmacist/doctor",RouteConfig.BASE_ROUTE);
		return restTemplate.getForObject(url, String.class);
	}
	
	/**
	 * 菜单页面的跳转
	 */
	@GetMapping("/sys/pharmacist")
	String drugAdministration(Model model) {
		String url = geturl("/sys/doctorPharmacist/pharmacist",RouteConfig.BASE_ROUTE);
		return restTemplate.getForObject(url, String.class);
	}
	
	/**
	 * insertBaseDoctor
	 * remote_bussiness
	 * 医生新增(模块:医生)
	 */ 
	@GetMapping("/insertBaseDoctor")
	public String insertBaseOrg(@RequestParam Map<String,Object> map){
		String value = "?doctorId={doctorId}&doctorName={doctorName}&orgId={orgId}&orgName={orgName}&subjectCode={subjectCode}&subjectName={subjectName}&titleCode={titleCode}&titleName={titleName}&label={label}&entryJobDate={entryJobDate}&personId={personId}&empno={empno}&sex={sex}&birth={birth}&phone={phone}&address={address}&introduction={introduction}&expertise={expertise}&remarks={remarks}&flag={flag}";
		String url = geturl("/insertBaseDoctor",RouteConfig.BUSSINESS_ROUTE)+value;
		return restTemplate.getForObject(url, String.class,map);
    }
	
	/**
	 * insertBasePharmacist
	 * remote_bussiness
	 * 药师新增(模块:药师)
	 */
	@GetMapping("/insertBasePharmacist")
	public String insertBasePharmacist(@RequestParam Map<String,Object> map){
		String value = "?pharmacistId={pharmacistId}&pharmacistName={pharmacistName}&orgId={orgId}&orgName={orgName}&subjectCode={subjectCode}&subjectName={subjectName}&titleCode={titleCode}&titleName={titleName}&label={label}&entryJobDate={entryJobDate}&personId={personId}&empno={empno}&sex={sex}&birth={birth}&phone={phone}&address={address}&introduction={introduction}&expertise={expertise}&remarks={remarks}&flag={flag}";
		String url = geturl("/insertBasePharmacist",RouteConfig.BUSSINESS_ROUTE)+value;
		return restTemplate.getForObject(url, String.class,map);
    }
	
	private String geturl(String path,String route){
		 ServiceInstance serviceInstance = loadBalancerClient.choose(route);
		 StringBuffer bf = new StringBuffer("http://");
		 bf.append(serviceInstance.getHost());
		 bf.append(":");
		 bf.append(serviceInstance.getPort());
		 bf.append(path);
		 return bf.toString();
	   }
}
